package itqf.dao;


import itqf.dao.impl.OrdersDao;
import itqf.entity.Address;
import itqf.entity.Orders;
import itqf.utils.DruidUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.itqf.dao
 * 2020/9/17
 */
public class OrdersDaoImpl implements OrdersDao {
    QueryRunner qr = new QueryRunner(DruidUtils.getDateSource());
    @Override
    public int addOrder(Orders orders) {
        String sql = "insert into orders (o_id,u_id,a_id,o_time,o_count,o_state) values(?,?,?,?,?,?)";
        try {
            return qr.update(sql,orders.getOid(),orders.getUid(),orders.getAid(),orders.getOtime(),orders.getOccount(),orders.getOstate());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Orders> findAllOrderByUid(int uid) {
        String sql = "select o_id as oid,o_time as otime,o_count as ocount,o_state as ostate,a_detail as adetail from orders o " +
                "inner join address a on a.a_id = o.a_id where o.u_id=?";
        try {
            List<Map<String, Object>> list = qr.query(sql, new MapListHandler(), uid);
            List<Orders> orders = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Orders o = new Orders();
                Address address = new Address();
                BeanUtils.populate(o,map);
                BeanUtils.populate(address,map);
                o.setAddress(address);
                orders.add(o);
            }
            return orders;
        } catch (Exception e) {
           throw new RuntimeException(e);
        }
    }

    @Override
    public Orders findOrderDetailByOid(String  oid) {
        String sql = "select o_id as oid,o_time as otime,o_count as occount,o_state as ostate,a_detail as adetail,a_phone as aphone," +
                "a_name as aname from orders o " +
                "inner join address a on a.a_id = o.a_id where o.o_id=?";
        try {
            Map<String, Object> map = qr.query(sql, new MapHandler(), oid);
            List<Orders> list= new ArrayList<>();
            Orders orders = new Orders();
            Address address = new Address();
            BeanUtils.populate(orders,map);
            BeanUtils.populate(address,map);
            orders.setAddress(address);
            return orders;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateOrderState(Orders orders) {
        String sql = "update  orders set o_state =?   where o_id=?";
        try {
            return qr.update(sql,orders.getOstate(),orders.getOid());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public int delCart(int uid, int pid) {
        String sql="delete from cart where u_id=? and p_id=?";

        try {
            return qr.update(sql,uid,pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
